package com.auto.survey.widget.dialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView
import com.auto.survey.R

class InfoDialog(context: Context) : Dialog(context, R.style.DialogTheme) {

    private var title: String? = null
    private var message: String? = null
    private var cancelText: String? = "取消"
    private var confirmText: String? = "确定"
    private var isCancelable = true
    private var isCanceledOnTouchOutside = true
    private var onCancelListener: (() -> Unit)? = null
    private var onConfirmListener: (() -> Unit)? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_info)

        // 设置点击外部和返回键行为
        setCancelable(isCancelable)
        setCanceledOnTouchOutside(isCanceledOnTouchOutside)

        // 初始化标题
        val tvTitle = findViewById<TextView>(R.id.tv_title)
        title?.let { tvTitle.text = it }

        // 初始化消息内容
        val tvMessage = findViewById<TextView>(R.id.tv_message)
        message?.let { tvMessage.text = it }

        // 初始化取消按钮
        val btnCancel = findViewById<Button>(R.id.btn_cancel)
        cancelText?.let { btnCancel.text = it }

        btnCancel.setOnClickListener {
            onCancelListener?.invoke()
            dismiss()
        }

        // 初始化确定按钮
        val btnConfirm = findViewById<Button>(R.id.btn_confirm)
        confirmText?.let { btnConfirm.text = it }

        btnConfirm.setOnClickListener {
            onConfirmListener?.invoke()
            dismiss()
        }

        // 设置窗口尺寸
        window?.setLayout(
            (context.resources.displayMetrics.widthPixels * 0.85).toInt(),
            WindowManager.LayoutParams.WRAP_CONTENT
        )
    }

    // 构建器模式
    class Builder(private val context: Context) {
        private val dialog = InfoDialog(context)

        fun setTitle(title: String): Builder {
            dialog.title = title
            return this
        }

        fun setMessage(message: String): Builder {
            dialog.message = message
            return this
        }

        fun setCancelText(text: String): Builder {
            dialog.cancelText = text
            return this
        }

        fun setConfirmText(text: String): Builder {
            dialog.confirmText = text
            return this
        }

        fun setCancelable(cancelable: Boolean): Builder {
            dialog.isCancelable = cancelable
            return this
        }

        fun setCanceledOnTouchOutside(canceledOnTouchOutside: Boolean): Builder {
            dialog.isCanceledOnTouchOutside = canceledOnTouchOutside
            return this
        }

        fun setOnCancelListener(listener: (() -> Unit)?): Builder {
            dialog.onCancelListener = listener
            return this
        }

        fun setOnConfirmListener(listener: (() -> Unit)?): Builder {
            dialog.onConfirmListener = listener
            return this
        }

        // 显示单选按钮版本
        fun showAsSingleButton(): Builder {
            dialog.findViewById<Button>(R.id.btn_cancel)?.visibility = View.GONE
            return this
        }

        fun show(): InfoDialog {
            dialog.show()
            return dialog
        }
    }
}